package kr.co.webmill;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;

public class Preferences extends Activity {
	CheckBox autoUpdate;
	Spinner updateFreqSpinner;
	Spinner magnitudeSpinner;
	public static final String USER_PREFERENCE = "USER_PREFERENCE";
	public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
	public static final String PREF_MIN_MAG = "PREF_MIN_MAG";
	public static final String PREF_UPDATE_FREQ = "PREF_UPDATE_FREQ";

	SharedPreferences prefs;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preferences);
		
		autoUpdate = (CheckBox) findViewById(R.id.checkbox_auto_update);
		updateFreqSpinner = (Spinner) findViewById(R.id.spinner_update_freq);
		magnitudeSpinner = (Spinner) findViewById(R.id.spinner_quake_mag);
		populateSpinners();
		prefs = getSharedPreferences(USER_PREFERENCE, Activity.MODE_PRIVATE);
		updateUIFromPreferences();
		
		Button okButton = (Button) findViewById(R.id.okButton);
		okButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				savePreferences();
				Preferences.this.setResult(RESULT_OK);
				finish();
			}
		});
		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Preferences.this.setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
	
	private void populateSpinners(){
		ArrayAdapter<CharSequence> fadapter;
		fadapter = ArrayAdapter.createFromResource(
				this, R.array.update_freq_options, android.R.layout.simple_spinner_item);
		fadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		updateFreqSpinner.setAdapter(fadapter);
		
		ArrayAdapter<CharSequence> mAdapter;
		mAdapter = ArrayAdapter.createFromResource(
				this, R.array.magnitude_options, android.R.layout.simple_spinner_item);
		mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		magnitudeSpinner.setAdapter(mAdapter);
		
 	}
	
	
	private void updateUIFromPreferences(){
		boolean autoUpChecked = prefs.getBoolean(PREF_AUTO_UPDATE, false);
		int updateFreqIndex = prefs.getInt(PREF_UPDATE_FREQ, 2);
		int minMagIndex = prefs.getInt(PREF_MIN_MAG, 0);
		autoUpdate.setChecked(autoUpChecked);
		updateFreqSpinner.setSelection(updateFreqIndex);
		magnitudeSpinner.setSelection(minMagIndex);
		
	}
	
	private void savePreferences(){
		boolean autoUpChecked = autoUpdate.isChecked();
		int updateFreqIndex = updateFreqSpinner.getSelectedItemPosition();
		int minMagIndex = magnitudeSpinner.getSelectedItemPosition();
		
		Editor editor = prefs.edit();
		editor.putBoolean(PREF_AUTO_UPDATE, autoUpChecked);
		editor.putInt(PREF_UPDATE_FREQ, updateFreqIndex);
		editor.putInt(PREF_MIN_MAG, minMagIndex);
		editor.commit();
		
	}


}
